package jp.sourceforge.akjrcp.videotools.dvdchapter;


import java.io.File;

import jp.sourceforge.akjrcp.akj_betas.Akj_betasPlugin;
import jp.sourceforge.akjrcp.util.StatusBarUtils;
import jp.sourceforge.akjrcp.videotools.VideoToolsSettingPage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.plugin.AbstractUIPlugin;


public class DVDChapterView extends ViewPart {
	public static Log log=LogFactory.getLog(DVDChapterView.class);
	
	public static final String ID = DVDChapterView.class.getName();

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	
	Composite composite;
	
	public void createPartControl(Composite parent) {
		//parent.setLayout(new GridLayout(1,false));
		Composite actionPanel=new Composite(parent,SWT.NULL);
		actionPanel.setLayout(new RowLayout());
		Button bt=new Button(actionPanel,SWT.NULL);
		ImageDescriptor opendirIcon = AbstractUIPlugin.imageDescriptorFromPlugin(
				Akj_betasPlugin.PLUGIN_ID, "icons/opendir.gif");
	    bt.setImage(opendirIcon.createImage());
	    bt.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
				
			}

			public void widgetSelected(SelectionEvent e) {
				openDir();
			}
	    	
	    });
	    
		parent.setLayout(new GridLayout());
		
		composite=new Composite(parent,SWT.NULL);
		composite.setLayout(new GridLayout(2,false));
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		
		

	}

	public void openDir(String path){
		if(path==null){
			return;
		}
		String mpath=Akj_betasPlugin.getDefault().getPreferenceStore().getString(VideoToolsSettingPage.PATH_MPLAYER);
			if(!mpath.endsWith("mplayer.exe")){
				StatusBarUtils.showStatusBar("mplayer not found");
				Akj_betasPlugin.getDefault().getProcessPrinter().printError("mplayer not found\n");
				return;
			}
			
		MPlayerDVDParser parser=new MPlayerDVDParser(mpath);
		DVDInfomation info=parser.parseDVD(new File(path));
		
		Control[] cs=composite.getChildren();
		for (int i = 0; i < cs.length; i++) {
			cs[i].dispose();
		}
		
		String chap[]=info.getChapters();
		log.info(chap.length);
		for (int i = 0; i < chap.length; i++) {
			Label label=new Label(composite,SWT.NULL);
			label.setText("Titleset "+(i+1));
			Text text=new Text(composite,SWT.READ_ONLY|SWT.BORDER);
			text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			text.pack();
			text.setText(chap[i]);
			
		}
		composite.pack();
		composite.getParent().layout(true);
		//composite.pack();
	}
	public void openDir(){
		DirectoryDialog dialog=new DirectoryDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
		String path=dialog.open();
		openDir(path);
	}
	
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}
	
}